package com.doradance.account.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doradance.R;

/**
 * Created by Administrator on 2018/3/14.
 */

public class CustomDialog extends Dialog {


    private static int mWidth;
    private static int mHeight;
    private static int mAfterX;
    private static int mAfterY;

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        if(mWidth > 0 && mHeight > 0 ) {
            layoutParams.width = mWidth;
            layoutParams.height = mHeight;
        }
        layoutParams.y = mAfterY;
        layoutParams.x = mAfterX;
        getWindow().setAttributes(layoutParams);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public static class Builder {
        private Context mContext;
        private String message;
        private View mContentView;

        private String positiveButtonText;
        private OnClickListener positiveButtonClickListener;

        public Builder(Context context) {
            mContext = context;
            mAfterX = 0;
            mAfterY = 0;
        }

        /** 设置Dialog宽高 */
        public Builder setDiglogMatrix(int width, int height) {
            mWidth = width;
            mHeight = height;
            return this;
        }

        /** 设置Dialog显示位置 */
        public Builder setDiglogShowAfter(int width, int height) {
            mAfterX = width;
            mAfterY = height;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = (String) mContext.getText(message);
            return this;
        }

//		public Builder setPositiveButton(int positiveButtonText,
//				OnClickListener listener) {
//			this.positiveButtonText = (String) mContext
//					.getText(positiveButtonText);
//			this.positiveButtonClickListener = listener;
//			return this;
//		}
//
//		public Builder setPositiveButton(String positiveButtonText,
//				OnClickListener listener) {
//			this.positiveButtonText = positiveButtonText;
//			this.positiveButtonClickListener = listener;
//			return this;
//		}

        public Builder setContentView(View view) {
            mContentView = view;
            return this;
        }

        public CustomDialog create() {
            final CustomDialog dialog = new CustomDialog(mContext, R.style.custom_dialog);
            View layout = View.inflate(mContext, R.layout.hpplay_custom_dialog_layout,null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            layout.findViewById(R.id.positiveputton)
                    .setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
//							if(positiveButtonClickListener != null){
//								positiveButtonClickListener.onClick(dialog,
//										DialogInterface.BUTTON_POSITIVE);
//							}
                            dialog.dismiss();
                        }
                    });
            if (message != null) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            }

            if (mContentView != null) {
                ((RelativeLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((RelativeLayout) layout.findViewById(R.id.content)).addView(
                        mContentView, new ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT));
            }

            dialog.setContentView(layout);
            return dialog;
        }
    }
}
